<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
            <el-form label-position="top" :model="editForm" :rules="editFormRules" ref="editForm" @submit.native.prevent>
                <input type="hidden"  v-model="editForm.id">
                <el-form-item label="常用电话名称：" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="常用电话号码：" prop="phone">
                    <el-input v-model="editForm.phone" auto-complete="off" placeholder="请输入电话"></el-input>
                </el-form-item>
                <el-form-item label-width="0" class="verifyidentity-button">
                    <el-button round @click="closeDialog()">取消</el-button>
                    <el-button type="primary" round @click="saveEdit('editForm')">确定</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<script>
import util from '@/utils/util'
import { qnToken} from '@/api/api'
import { updateCUsedP} from '@/api/communicat'

export default {
  name:'UsefulTelSetForm',
  props: {
    editInfo: {
        type: Object,
        required: false
    },
    closeDialog: {
        type: Function,
        required: true
    },
    organizationId: {
        type: Number,
        required: true
    },
  },
  data() {
    return {
        defaultImg: this.GLOBAL.defaultImg,
        util:util,
        orgOptions: [],
        editForm: {
            id: 0,
            organizationId:this.organizationId,
            name:'',
            phone:'',
        },
        editFormRules: {
            name: [
                { required: true, message: '请输入常用电话名称', trigger: 'blur' },
            ],
            phone: [
                { required: true, message: '请输入电话号码', trigger: 'blur' },
            ],
        },
    }
  },
  created() {
      let that = this
      let infoObj = this.editInfo
      if(infoObj.id) {//编辑
        this.editForm.id = infoObj.id
        this.editForm.name = infoObj.name
        this.editForm.phone = infoObj.phone
      }
  },
  methods: {
    saveEdit(formName) {
        // 阻止事件冒泡
        event.stopPropagation()
        let that = this
        that.$refs[formName].validate((valid) => {
          if (valid) {
            let parames = that.editForm
            updateCUsedP(parames).then(res => {
                if(res.status === 200) {
                    that.$message({
                        message: '保存成功！',
                        type: 'success'
                    })
                    that.closeDialog(true)
                } else {
                    that.$message({
                        message: res.info,
                        type: 'warning'
                    })
                }
            });
          } else {
              return false
          }
        });
    },
  }
}
</script>
